package com.wings4.number.utils;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2/20/14
 * Time: 8:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class SpellingUtil
{
    String[] unitdo ={"", " One", " Two", " Three", " Four", " Five",
            " Six", " Seven", " Eight", " Nine", " Ten", " Eleven", " Twelve",
            " Thirteen", " Fourteen", " Fifteen",  " Sixteen", " Seventeen",
            " Eighteen", " Nineteen"};
    String[] tens =  {"", "Ten", " Twenty", " Thirty", " Forty", " Fifty",
            " Sixty", " Seventy", " Eighty"," Ninety"};
    String[] digit = {"", " Hundred", " Thousand", " Lakh", " Crore"};
    long r;


    //Count the number of digits in the input number
    long numberCount(long num)
    {
        long cnt=0;

        while (num>0)
        {
            r = num%10;
            cnt++;
            num = num / 10;
        }

        return cnt;
    }


    //Function for Conversion of two digit

    String twonum(long numq)
    {
        long numr, nq;
        String ltr="";

        nq = numq / 10;
        numr = numq % 10;

        if (numq>19)
        {
            ltr=ltr+tens[(int)nq]+unitdo[(int)numr];
        }
        else
        {
            ltr = ltr+unitdo[(int)numq];
        }

        return ltr;
    }

    //Function for Conversion of three digit

    String threenum(long numq)
    {
        long numr, nq;
        String ltr = "";


        nq = numq / 100;
        numr = numq % 100;

        if (numr == 0)
        {
            ltr = ltr + unitdo[(int)nq]+digit[1];
        }
        else
        {
            ltr = ltr +unitdo[(int)nq]+digit[1]+" and"+twonum(numr);
        }
        return ltr;

    }

    //Function for Conversion of three digit

    String threenumAfterCrore(long numq)
    {
        long numr, nq;
        String ltr = "";


        nq = numq / 100;
        numr = numq % 100;
        if(numq<=9){
           nq=numq;
        }
        if (numr == 0 || numq<=9)
        {
            ltr = ltr + unitdo[(int)nq];
        }
        else
        {
            ltr = ltr +unitdo[(int)nq]+digit[1]+" "+twonum(numr);
        }
        return ltr;

    }


    //Function for Conversion of three digit

    String fournum(long numq)
    {
        long numr, nq;
        String ltr = "";


        nq = numq / 1000;
        numr = numq % 1000;

        if (numr == 0)
        {
            ltr = ltr + unitdo[(int)nq]+digit[2];
        }
        else
        {
            ltr = ltr +unitdo[(int)nq]+digit[2]+" "+threenum(numr);
        }
        return ltr;

    }

    //Function for Conversion of three digit

    String fivenum(long numq)
    {
        long numr, nq;
        String ltr = "";


        nq = numq / 10000;
        numr = numq % 10000;

        if (numr == 0)
        {
            ltr = ltr + unitdo[(int)nq]+digit[3];
        }
        else
        {
            ltr = ltr +unitdo[(int)nq]+digit[3]+" "+fournum(numr);
        }
        return ltr;

    }

    String largenum(long numq)
    {
        long numr, nq;
        String ltr = "";
        String number=""+numq;
        int size=number.length();

        int denom=1;
        for (int i = 0; i < size-1; i++) {
            denom*=10;
        }
        nq = numq / denom;
        numr = numq % denom;

        if (numr == 0)
        {
            ltr = ltr + unitdo[(int)nq]+digit[1];
        }
        else
        {
            if(numr>99){
                ltr = ltr +unitdo[(int)nq]+digit[size-2]+" "+largenum(numr);
            }  else{
                ltr = ltr +unitdo[(int)nq]+digit[1]+" and "+twonum(numr);
            }

        }
        return ltr;

    }
    public static String convertAmountToNumber(long amount){

        //Defining variables q is quotient, r is remainder

        long len, q=0, r=0;
        String ltr = " ";
        String Str = "";
        SpellingUtil n = new SpellingUtil();
        long num = amount;

        if (num <= 0) System.out.println("Zero or Negative number not for conversion");

        while (num>0)
        {

            len = n.numberCount(num);

            //Take the length of the number and do letter conversion

            switch ((int)len)

            {

                case 8:
                    q=num/10000000;
                    r=num%10000000;
                    ltr = n.twonum(q);
                    Str = Str+ltr+n.digit[4];
                    num = r;
                    break;

                case 7:
                case 6:
                    q=num/100000;
                    r=num%100000;
                    ltr = n.twonum(q);
                    Str = Str+ltr+n.digit[3];
                    num = r;
                    break;

                case 5:
                case 4:

                    q=num/1000;
                    r=num%1000;
                    ltr = n.twonum(q);
                    Str= Str+ltr+n.digit[2];
                    num = r;
                    break;

                case 3:


                    if (len == 3)
                        r = num;
                    ltr = n.threenum(r);
                    Str = Str + ltr;
                    num = 0;
                    break;

                case 2:

                    ltr = n.twonum(num);
                    Str = Str + ltr;
                    num=0;
                    break;

                case 1:
                    Str = Str + n.unitdo[(int)num];
                    num=0;
                    break;
                default:

                    long largeNum=num/10000000;
                    while(largeNum>0){
                        long largeLen, largeQ=0, largeR=0;
                        largeLen = n.numberCount(largeNum);
                        switch ((int)largeLen)

                        {


                            case 9:

                            case 8:
                                largeQ=largeNum/10000000;
                                largeR=largeNum%10000000;
                                ltr = n.twonum(largeQ);
                                Str = Str+ltr+n.digit[4];
                                largeNum = largeR;
                                break;

                            case 7:
                            case 6:
                                largeQ=largeNum/100000;
                                largeR=largeNum%100000;
                                ltr = n.twonum(largeQ);
                                Str = Str+ltr+n.digit[3];
                                largeNum = largeR;
                                break;

                            case 5:
                            case 4:

                                largeQ=largeNum/1000;
                                largeR=largeNum%1000;
                                ltr = n.twonum(largeQ);
                                Str= Str+ltr+n.digit[2];
                                largeNum = largeR;
                                break;

                            case 3:


                                if (largeLen == 3)
                                    largeR = largeNum;
                                ltr = n.threenum(largeR);
                                Str = Str + ltr;
                                largeNum = 0;
                                break;

                            case 2:

                                ltr = n.twonum(largeNum);
                                Str = Str + ltr;
                                largeNum=0;
                                break;

                            case 1:
                                Str = Str + n.unitdo[(int)largeNum];
                                largeNum=0;
                                break;
                        }



                    }
                    r=num%10000000;
                    // ltr = n.threenumAfterCrore(q);
                    Str = Str+n.digit[4];
                    num = r;
                    break;


            }

        }
        return Str;
    }
    public static void main(String[] args) throws Exception
    {


        long []numbers={1,10,110,1110,11110,111110,1111110,11111110,111111110,1111111110,1111111110};
        numbers[10]=Long.parseLong("9999999999999999");
        for (int i = 0; i < numbers.length; i++) {
            long number = numbers[i];
            System.out.println("convertAmountToNumber("+number+") = " + convertAmountToNumber(number));
        }

    }

}

